package br.ucb.tcc.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;

import net.java.dev.designgridlayout.DesignGridLayout;
import thinlet.swing.border.LineBorder;
import thinlet.swing.container.ToolFooter;
import thinlet.swing.table.Cell;
import thinlet.swing.table.Table;
import br.ucb.tcc.entidades.Relacionamento;
import br.ucb.tcc.persistencia.TwitterDAO;
import br.ucb.tcc.utils.Constantes;
import br.ucb.tcc.utils.ExcelUtil;

/**
 * @author italoricci
 * 
 */
public class ListarRelacoesBancoPanel extends AbstractPanel implements Table.Listener<Relacionamento>, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Table<Relacionamento> lista;
	private List<Relacionamento> listas = new ArrayList<Relacionamento>();
	private ToolFooter subPanelHeader;
	private JScrollPane scrollPane;
	private JButton exportarParaExcelButton;
	private JComboBox comboTags;
	private JButton buttonPesquisa;

	public ListarRelacoesBancoPanel() {
		super(new BorderLayout());
		JPanel header = new JPanel(new BorderLayout());
		subPanelHeader = new ToolFooter();
		JLabel lbHeader = new JLabel("<html><body>" + "<table><tr><td>" + "<img width='650' height='100' src='"
				+ imgsrc + "'></img>" + "</td><td><h3>Listagem de relações</h3></td></tr></table></body></html>");
		lbHeader.setBorder(new LineBorder(Color.lightGray, 0, 0, 1, 0, 0, 8, 8, 8));
		lbHeader.setBackground(new Color(0xdddddd));
		lbHeader.setOpaque(true);
		header.add(lbHeader, BorderLayout.CENTER);

		JPanel paneilPesquisa = new JPanel();
		DesignGridLayout designGridLayout = new DesignGridLayout(paneilPesquisa);
		designGridLayout.row().grid().add(lbHeader);
		designGridLayout.row().grid(new JLabel("Tag")).add(getComboTags());
		designGridLayout.row().center().add(getButtonPesquisa());
		subPanelHeader.add(paneilPesquisa);

		add(paneilPesquisa, BorderLayout.NORTH);
		popularLista();
		scrollPane = new JScrollPane(lista);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		add(scrollPane, BorderLayout.CENTER);

		JPanel panelGerarExcel = new JPanel();
		DesignGridLayout designGridLayout2 = new DesignGridLayout(panelGerarExcel);
		designGridLayout2.row().center().add(getExportarParaExcelButton());

		add(panelGerarExcel, BorderLayout.SOUTH);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doubleClicked(Relacionamento RelacoesDTO) {
		JLabel lbHeader = new JLabel(montarDetalhamentoRelacao(RelacoesDTO));
		lbHeader.setBorder(new LineBorder(Color.lightGray, 0, 0, 1, 0, 0, 8, 8, 8));
		lbHeader.setBackground(new Color(0xdddddd));
		lbHeader.setOpaque(true);
		JOptionPane.showMessageDialog(null, lbHeader, "Detalhamento de Usuário", JOptionPane.PLAIN_MESSAGE, null);
	}

	public void popularLista() {
		lista = new Table<Relacionamento>();
		lista.setCells(Relacionamento.class, new Cell("nomeOrigem").setColor(new Color(0x4a77ce)), new Cell(
				"nomeDestino").setColor(new Color(0x4a77ce)),
				new Cell("hashTagPesquisa").setColor(new Color(0x4a77ce)), new Cell("tweetRelacao").setColor(new Color(
						0x4a77ce)));
		lista.addListListener(this);
	}

	public void updateLista(List<Relacionamento> relacoes) {
		popularLista();
		this.remove(scrollPane);
		for (Relacionamento rel : relacoes) {
			lista.addItem(rel);
		}
		scrollPane = new JScrollPane(lista);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		add(scrollPane, BorderLayout.CENTER);
		this.revalidate();
	}

	public void limparLista() {
		lista.setModel(null);
		lista.repaint();
		this.revalidate();
	}

	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
		}
		return scrollPane;
	}

	public ToolFooter getSubPanelHeader() {
		if (subPanelHeader == null) {
			subPanelHeader = new ToolFooter();
		}

		return subPanelHeader;
	}

	@Override
	public void selectionChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub

	}

	public JButton getExportarParaExcelButton() {
		if (exportarParaExcelButton == null) {
			exportarParaExcelButton = new JButton("Export para Excel");
			exportarParaExcelButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					ExcelUtil<Relacionamento> exportador = new ExcelUtil<Relacionamento>();
					exportador.montarExcel(listas, "idOrigem", "nomeOrigem", "idDestino", "nomeDestino",
							"tweetRelacao", "hashTagPesquisa");
					mostrarMensagemInformativa("Arquivo gerado com sucesso no seguinte caminho>>" + Constantes.PATH
							+ Constantes.NOME_EXCEL_GERADO, "Mensagem");
				}
			});
		}
		return exportarParaExcelButton;
	}

	/**
	 * @return the comboLocalizacao
	 */
	public JComboBox getComboTags() {
		if (comboTags == null) {
			comboTags = new JComboBox();
			TwitterDAO dao = new TwitterDAO();
			Set<String> tags = dao.buscarTagsPesquisadas();
			comboTags.addItem("");
			for (String string : tags) {
				comboTags.addItem(string);
			}
		}
		return comboTags;
	}

	public JButton getButtonPesquisa() {
		if (buttonPesquisa == null) {
			buttonPesquisa = new JButton("Pesquisar");

			buttonPesquisa.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					listas = new TwitterDAO().buscarRelacoesPorTags((String) getComboTags().getSelectedItem());
					updateLista(listas);
				}
			});

		}
		return buttonPesquisa;
	}
}
